// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    //extends: cc.Component,
    __ctor__: function (row, column) {
        //this.type = Math.floor(Math.random() * 4);
        this.type = 0;
        console.log(this.type);
        this.row = row;
        this.column = column;
        this.backGround = cc.find("Canvas/PuzzleBG");
        this.content = cc.find("Canvas/PuzzleChooseScrollView/view/content");
        this.main = cc.find("Canvas").getComponent("Main");

        //var self = this;
        console.log("struct");
    },
    properties: {
        //行
        row: 0,
        //列
        column: 0,
        //当前拼图资源
        texture: {
            default: null,
            type: cc.Texture2D,
        },
        //遮罩图集
        spriteAtlas: {
            default: null,
            type: cc.SpriteAtlas,
        },
        //所有小块数组
        spriteArray: {
            default: null,
        },
        //废片数组
        useless: {
            default: null,
        },
        //排列类型
        type: {
            default: -1,  //初始-1, 0：不做翻转，1：左右翻转，2：上下翻转，3：对角线翻转
        },
        //背景板
        backGround: {
            default: null,
        },
        //一共多少块
        num: {
            get() {
                return this._bar;
            },
            set(value) {
                this._bar = value;
                //switch (this.type) {
                //case 0:
                var dddd = 0;
                this.spriteArray = new Array(this.row);
                for (let i = 0; i < this.spriteArray.length; i++) {
                    this.spriteArray[i] = new Array(this.column);
                    for (let j = 0; j < this.spriteArray[i].length; j++) {
                        let index = (i * 6 + j + 1).toString();       //图片名1-48
                        let name = (i + 1) + "_" + (j + 1);
                        let newNode = new cc.Node(index.toString());
                        newNode.width = this.texture.width / this.column;
                        newNode.height = this.texture.height / this.row;
                        let maskNode = new cc.Node(index.toString());
                        let id = 0;
                        let spriteFrame = null;
                        let pixelx = 0;
                        let pixely = 0;
                        let childposx = 0;
                        let childposy = 0;
                        let offsetx = 0;
                        let offsety = 0;
                        let backposx = newNode.width * j + newNode.width / 2;
                        let backposy = newNode.height * i + newNode.height / 2;
                        // let backposx = 0;
                        // let backposy = 0;

                        switch (this.type) {
                            case 0:
                                id = this.num - i * this.column - (this.column - 1 - j);  //id对应图片名
                                //name = (i + 1) + "_" + (j + 1);
                                //maskNode = new cc.Node(index.toString());
                                spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                maskNode.width = spriteFrame.getOriginalSize().width;
                                maskNode.height = spriteFrame.getOriginalSize().height;
                                //newNode.setScale(1, 1);
                                // backposx = newNode.width * (this.row - i - 1) + newNode.width / 2;
                                // backposy = newNode.height * j + newNode.height / 2;
                                pixelx = defaultList[this.num][this.row - i - 1][j].x;
                                pixely = this.texture.height - defaultList[this.num][this.row - i - 1][j].y - maskNode.height;
                                childposx = defaultList[this.num][this.row - i - 1][j].x + maskNode.width / 2;
                                childposy = defaultList[this.num][this.row - i - 1][j].y + maskNode.height / 2;
                                // backposx = newNode.width * i + newNode.width / 2;
                                // backposy = newNode.height * j + newNode.height / 2;
                                break;
                            case 1:
                                id = this.num - i * this.column - j;  //id对应图片名
                                //name = (i + 1) + "_" + (j + 1);
                                //maskNode = new cc.Node(index.toString());
                                spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                maskNode.width = spriteFrame.getOriginalSize().width;
                                maskNode.height = spriteFrame.getOriginalSize().height;
                                maskNode.setScale(-1, 1);
                                // backposx = newNode.width * (this.row - i - 1) + newNode.width / 2;
                                // backposy = newNode.height * (this.column - j - 1) + newNode.height / 2;
                                pixelx = this.texture.width - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - maskNode.width;
                                pixely = this.texture.height - defaultList[this.num][this.row - i - 1][this.column - j - 1].y - maskNode.height;
                                childposx = this.texture.width - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - maskNode.width / 2;
                                childposy = defaultList[this.num][this.row - i - 1][this.column - j - 1].y + maskNode.height / 2;
                                break;
                            case 2:
                                id = i * this.column + j + 1;  //id对应图片名
                                //name = (i + 1) + "_" + (j + 1);
                                //maskNode = new cc.Node(index.toString());
                                spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                maskNode.width = spriteFrame.getOriginalSize().width;
                                maskNode.height = spriteFrame.getOriginalSize().height;
                                maskNode.setScale(1, -1);
                                // backposx = newNode.width * i + newNode.width / 2;
                                // backposy = newNode.height * j + newNode.height / 2;
                                pixelx = defaultList[this.num][i][j].x;
                                pixely = defaultList[this.num][i][j].y;
                                childposx = defaultList[this.num][i][j].x + maskNode.width / 2;
                                childposy = this.texture.height - defaultList[this.num][i][j].y - maskNode.height / 2;
                                break;
                            case 3:
                                id = this.num - i * this.column - j;  //id对应图片名
                                name = (this.row - i) + "_" + (j + 1);
                                //maskNode = new cc.Node(index.toString());
                                spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                maskNode.width = spriteFrame.getOriginalSize().width;
                                maskNode.height = spriteFrame.getOriginalSize().height;
                                maskNode.setScale(-1, -1);
                                // backposx = newNode.width * (this.row - i - 1) + newNode.width / 2;
                                // backposy = newNode.height * (this.column - j - 1) + newNode.height / 2;
                                pixelx = this.texture.width - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - maskNode.width;
                                pixely = defaultList[this.num][this.row - i - 1][this.column - j - 1].y;
                                childposx = this.texture.width - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - maskNode.width / 2;
                                childposy = this.texture.height - defaultList[this.num][this.row - i - 1][this.column - j - 1].y - maskNode.height / 2;

                                break;
                            default:
                                break;
                        }





                        let backNode = cc.instantiate(newNode);; //背景位置节点
                        backNode.name = name;
                        let childNode = cc.instantiate(maskNode);
                        newNode.addChild(maskNode);
                        backNode.setParent(this.backGround);
                        backNode.setPosition(backposx - 360, backposy + 40);
                        //backNode.addComponent(cc.Sprite).spriteFrame = spriteFrame;
                        let mask = maskNode.addComponent(cc.Mask);
                        mask.type = cc.Mask.Type.IMAGE_STENCIL;
                        mask.spriteFrame = spriteFrame;
                        mask.alphaThreshold = 0.5;
                        //mask.resize();
                        maskNode.addChild(childNode);
                        offsetx = childposx - backposx;
                        offsety = childposy - backposy;
                        // console.log("差值");
                        // console.log(offsetx + "," + offsety);
                        maskNode.setPosition(offsetx, offsety);
                        let sprite = childNode.addComponent(cc.Sprite);
                        // console.log(pixelx + "," + pixely);
                        // console.log(childNode.width + "," + childNode.height);
                        let frame = new cc.SpriteFrame(this.texture, cc.rect(pixelx, pixely, childNode.width, childNode.height), false, cc.v2(0, 0), cc.size(childNode.width, childNode.height));
                        //sprite.spriteFrame = frame;
                        //console.log(frame);
                        this.spriteArray[i][j] = { name: index, row: i + 1, column: j + 1, node: newNode };

                        this.spriteArray[i][j].node.setParent(this.content);
                        let drag = this.spriteArray[i][j].node.addComponent("Drag");
                        // drag.width = i + 1;
                        // drag.height = j + 1;
                        // console.log(name);
                        drag.name = name;
                        //this.spriteArray[i][j].node.position = cc.v2(index * 200, 20);
                        this.main.startpuzzle.push(this.spriteArray[i][j].node);
                        // this.spriteArray[i][j].node.setParent(this.backGround);
                        // this.spriteArray[i][j].node.position = backNode.position;
                        //   console.log(backNode.name);
                    }
                }

            }
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        var index = Math.floor(Math.random() * 4) + 1;
        var self = this;
        cc.loader.loadRes("background/img" + index, cc.Texture2D, function (err, texture) {
            self.texture = texture;
            console.log(texture.getPixelFormat());
            console.log(texture);
            cc.loader.loadRes("Atlas/atlas" + self.row * self.column, cc.SpriteAtlas, function (err, atlas) {
                console.log(atlas);
                self.spriteAtlas = atlas;
                self.num = self.row * self.column;
                self.generateUseless(self.column);
                self.randomSortAndRotate();
                self.addBGPuzzle();
                console.log(self.main.startpuzzle);
            });
        });
    },

    // update (dt) {},

    //生成废片
    generateUseless(num) {
        console.log(num);
        this.useless = new Array(num);
        for (let i = 0; i < num; i++) {
            let id = Math.floor(Math.random() * this.num) + 1;  //id对应图片名
            let index = (this.num + i + 1).toString();       //图片名
            let newNode = new cc.Node(index.toString());
            let name = "0_0";
            newNode.width = this.texture.width / this.column;
            newNode.height = this.texture.height / this.row;
            let maskNode = new cc.Node(index.toString());
            let spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
            maskNode.width = spriteFrame.getOriginalSize().width;
            maskNode.height = spriteFrame.getOriginalSize().height;
            let childNode = cc.instantiate(maskNode);
            newNode.addChild(maskNode);
            //let backNode = cc.instantiate(newNode); //背景位置节点
            //backNode.setParent(this.backGround);
            //backNode.setPosition(defaultList[this.num][this.row - i - 1][j].x + newNode.width / 2, defaultList[this.num][this.row - i - 1][j].y + newNode.height / 2);
            let mask = maskNode.addComponent(cc.Mask);
            mask.type = cc.Mask.Type.IMAGE_STENCIL;
            mask.spriteFrame = spriteFrame;
            mask.alphaThreshold = 0.5;
            //mask.resize();
            maskNode.addChild(childNode);
            let sprite = childNode.addComponent(cc.Sprite);
            let posx = Math.floor(Math.random() * 320) + 200;
            let posy = Math.floor(Math.random() * 560) + 200;
            let frame = new cc.SpriteFrame(this.texture, cc.rect(posx, posy, maskNode.width, maskNode.height), false, cc.v2(0, 0), cc.size(maskNode.width, maskNode.height));
            sprite.spriteFrame = frame;
            this.useless[i] = { name: index.toString(), row: -1, column: -1, node: newNode };
            this.useless[i].node.setParent(this.content);
            let drag = this.useless[i].node.addComponent("Drag");
            // drag.width = -1;
            // drag.height = -1;
            drag.name = "0_0";
            //this.useless[i].node.position = cc.v2((i + 48) * 200, 20);
            //this.useless[i].node.position = cc.v2(i * 200, 20);
            this.main.startpuzzle.push(this.useless[i].node);
        }
    },
    //随机排序、随机旋转和定位置
    randomSortAndRotate() {
        var newArray = [];
        for (let i = 0; i < (this.row + 1) * this.column; i++) {
            let ran = Math.floor(Math.random() * this.main.startpuzzle.length);
            let ranrot = Math.floor(Math.random() * 4) * 90;
            newArray.push(this.main.startpuzzle[ran]);
            this.main.startpuzzle.splice(ran, 1);
            //newArray[i].position = cc.v2(100 + i * 200, 20);   
            let drag = newArray[i].getComponent("Drag");
            // console.log(drag);
            //    console.log(drag.startpos);
            // console.log(newArray[i]);
            //newArray[i].setParent(this.content);
            newArray[i].position = cc.v2(drag.startpos[i].x, drag.startpos[i].y);
            newArray[i].rotation = ranrot;
        }
        
        this.content.position = cc.v2(this.content.width / 2 - 360, this.content.position.y);
        this.main.startpuzzle = newArray;
    },
    //添加背景块数组
    addBGPuzzle() {
        let bgchildren = cc.find("Canvas/PuzzleBG").getChildren();
        console.log(bgchildren);
        for (let i = 0; i < bgchildren.length; i++) {
            this.main.puzzlebgChildren.push(bgchildren[i]);
        }
        console.log(this.main.puzzlebgChildren);
        for (let i = 0; i < this.row; i++) {
            this.main.puzzleBG2.push([]);
            for (let j = 0; j < this.column; j++) {
                this.main.puzzleBG2[i].push(this.main.puzzlebgChildren[j]);

            }
            this.main.puzzlebgChildren.splice(0, this.column);
        }
        console.log(this.main.puzzleBG2);
        for (let j = 0; j < (this.row + 1) * this.column; j++) {
            let drag = this.main.startpuzzle[j].getComponent("Drag");
            drag.startpuzzle = this.main.startpuzzle;
            drag.puzzleBG2 = this.main.puzzleBG2;
            drag.isLock = this.main.isLock;
            //   console.log(drag.puzzleBG2);

            for (let i = 0; i < bgchildren.length; i++) {
                drag.puzzleBG.push(bgchildren[i]);
            }
        }


    },
});

var defaultList = {
    48: [
        [
            { x: 0, y: 807 },
            { x: 112, y: 835 },
            { x: 210, y: 811 },
            { x: 352, y: 839 },
            { x: 473, y: 808 },
            { x: 567, y: 829 },
        ],
        [
            { x: 0, y: 711 },
            { x: 87, y: 711 },
            { x: 206, y: 713 },
            { x: 331, y: 717 },
            { x: 469, y: 687 },
            { x: 589, y: 684 },
        ],
        [
            { x: 0, y: 593 },
            { x: 91, y: 592 },
            { x: 231, y: 593 },
            { x: 362, y: 594 },
            { x: 470, y: 592 },
            { x: 588, y: 595 },
        ],
        [
            { x: 0, y: 450 },
            { x: 89, y: 443 },
            { x: 237, y: 475 },
            { x: 329, y: 471 },
            { x: 477, y: 451 },
            { x: 565, y: 455 },
        ],
        [
            { x: 0, y: 361 },
            { x: 110, y: 353 },
            { x: 208, y: 329 },
            { x: 351, y: 337 },
            { x: 475, y: 357 },
            { x: 565, y: 353 },
        ],
        [
            { x: 0, y: 211 },
            { x: 88, y: 239 },
            { x: 207, y: 232 },
            { x: 358, y: 204 },
            { x: 445, y: 230 },
            { x: 589, y: 204 },
        ],
        [
            { x: 0, y: 91 },
            { x: 118, y: 120 },
            { x: 232, y: 98 },
            { x: 331, y: 92 },
            { x: 473, y: 118 },
            { x: 571, y: 118 },
        ],
        [
            { x: 0, y: 0 },
            { x: 86, y: 0 },
            { x: 236, y: 0 },
            { x: 351, y: 0 },
            { x: 445, y: 0 },
            { x: 595, y: 0 },
        ],
    ],
    35: [],
    24: [],
}
